package com.sinodata.bsm.cicp.collector.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: Shell返回Object类型指标采集器（简单对象指标采集器）
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-1-7 下午3:38:41          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ShellReturnObjectCollector extends Collector {
    private static Logger logger = Logger.getLogger(ShellReturnObjectCollector.class);

    @Override
    public String[] getPropMeta() {
        return new String[] { "$1" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "collectCmd", "Regex", "charactSet" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String shell = this.paramMap.get("collectCmd");
        String regex = this.paramMap.get("Regex");
        String charactSet = this.paramMap.get("charactSet");
        Pattern pattern = Pattern.compile(regex);
        int groupCount = pattern.matcher("").groupCount();
        Matcher matcher = null;
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0, charactSet);
            String strLine = null;
            StringBuilder sb = new StringBuilder();
            while ((strLine = reader.readLine()) != null) {
                strLine = strLine.trim();
                matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    for (int i = 1; i < groupCount; i++) {
                        sb.append(matcher.group(i)).append(SeparatorConstants.COL_SEPARATOR);
                    }
                    sb.append(matcher.group(groupCount)).append(SeparatorConstants.LIN_SEPARATOR);
                }
            }
            if (reader != null) {
                reader.close();
            }
            String result = sb.toString().replace("null", "");
            if (result.endsWith(SeparatorConstants.LIN_SEPARATOR)) {
                result = result.substring(0, result.length() - 3);
            }
            ResPropertyValue resPropertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[1];
            propValues[0] = ResPropertyValue.createPropValue(result, this.getPropMeta()[0]);
            resPropertyValueX.setValues(propValues);
            values.add(resPropertyValueX);
        } catch (IOException e) {
            logger.error("ShellReturnObjectCollector product error,ip[" + ip + "],shell[" + shell + "],regex[" + regex + "]", e);
            throw new CollectorException(e);
        } finally {
            IOUtils.closeQuietly(reader);
        }
        return values;
    }
}
